package com.qdxwx.manage.controller.system;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.system.SystemMsgService;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.system.SystemMsg;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 站内信Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/system/msg")
public class SystemMsgEditController extends BaseController {

    @Autowired
    private SystemMsgService systemMsgService;

    @Permissions("msg:add")
    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult edit(HttpServletRequest request) {
        String content = TKString.trim(request.getParameter("content"));
        if(TKString.isEmpty(content)) return error("请输入信息");
        SystemMsg systemMsg = new SystemMsg();
        systemMsg.setStatus(0);
        systemMsg.setContent(content);
        return toAjax(systemMsgService.insert(systemMsg));
    }

    @Permissions("msg:del")
    @PostMapping("/del.json")
    @ResponseBody
    public AjaxResult del(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if(id == 0) return error("参数异常");
        SystemMsg systemMsg = systemMsgService.get(id);
        if(systemMsg == null) return error("找不到指定站内信，id="+id);
        return toAjax(systemMsgService.delete(id));
    }

}
